package org.mspring.platform.web.freemarker.widget.render;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.mspring.platform.cache.Cache;
import org.mspring.platform.cache.CacheManager;
import org.mspring.platform.web.freemarker.widget.WidgetConstants;
import org.mspring.platform.web.freemarker.widget.config.WidgetConfig;
import org.mspring.platform.web.freemarker.widget.http.HttpWidget;
import org.mspring.platform.web.freemarker.widget.http.HttpWidgetRequest;
import org.mspring.platform.web.freemarker.widget.http.HttpWidgetResponse;
import org.mspring.platform.web.freemarker.widget.http.impl.DefaultHttpWidget;
import org.mspring.platform.web.freemarker.widget.utils.WidgetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Gao Youbo
 * @since 2012-12-5
 */
public abstract class AbstractWidgetRender implements WidgetRender {

    private static final Logger log = LoggerFactory.getLogger(AbstractWidgetRender.class);

    @Override
    public void render(WidgetConfig widgetConfig, CacheManager cacheManager, HttpWidgetRequest widgetRequest, HttpWidgetResponse widgetResponse) {
        preWidgetRender(widgetConfig, cacheManager, widgetRequest, widgetResponse);
        nativeWidgetRender(widgetConfig, cacheManager, widgetRequest, widgetResponse);
        postWidgetRender(widgetConfig, cacheManager, widgetRequest, widgetResponse);
    }

    /**
     * 执行render
     *
     * @param widgetConfig
     * @param cacheManager
     * @param widgetRequest
     * @param widgetResponse
     */
    protected abstract void nativeWidgetRender(WidgetConfig widgetConfig, CacheManager cacheManager, HttpWidgetRequest widgetRequest, HttpWidgetResponse widgetResponse);

    /**
     * render之前执行的操作
     *
     * @param widgetConfig
     * @param cacheManager
     * @param widgetRequest
     * @param widgetResponse
     */
    protected void preWidgetRender(WidgetConfig widgetConfig, CacheManager cacheManager, HttpWidgetRequest widgetRequest, HttpWidgetResponse widgetResponse) {
    }

    /**
     * render之后执行的操作
     *
     * @param widgetConfig
     * @param cacheManager
     * @param widgetRequest
     * @param widgetResponse
     */
    protected void postWidgetRender(WidgetConfig widgetConfig, CacheManager cacheManager, HttpWidgetRequest widgetRequest, HttpWidgetResponse widgetResponse) {
        HttpWidget httpWidget = new DefaultHttpWidget(widgetRequest, widgetResponse);
        WidgetUtils.setHttpWidget(widgetRequest, widgetConfig, httpWidget);

        if (widgetConfig.isCache()) {
            String value = widgetResponse.getResponseAsString();
            if (cacheManager != null) {
                Cache cache = cacheManager.getCache(WidgetConstants.WIDGET_CACHE_NAME);
                if (cache != null) {
                    cache.set(widgetConfig.getName(), value, widgetConfig.getTtl());
                }
            }
        }
    }

    protected HttpWidget getHttpWidgetCache(WidgetConfig widgetConfig, CacheManager cacheManager, HttpWidgetRequest widgetRequest, HttpWidgetResponse widgetResponse) {
        HttpWidget httpWidget = null;
        if (widgetConfig.isCache()) {
            String value = null;
            if (cacheManager != null) {
                Cache cache = cacheManager.getCache(WidgetConstants.WIDGET_CACHE_NAME);
                if (cache != null) {
                    value = cache.get(widgetConfig.getName());
                }
            }
            // if (value != null) {
            if (StringUtils.isNotBlank(value)) {
                try {
                    widgetResponse.setResponseContent(value);
                    httpWidget = new DefaultHttpWidget(widgetRequest, widgetResponse);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return httpWidget;
    }

}
